<section id="content">
<script type="text/javascript">

function validateEmail(email) { 
    var re = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/;
    return re.test(email);
}

function backToIndex(){
	return $("#dummyForm").submit();
}

function facultyConfirm(){
	isValidInfo = true;
	$("#nameMsg").html("");
	$("#emailMsg").html("");
	$("#fileMsg").html("");
	if($("#txtFacultyName").val() == ""){
		$("#nameMsg").html("(Vui lòng nhập tên giảng viên)");
		isValidInfo = false;
	}
	if($("#txtFacultyEmail").val() == ""){
		$("#emailMsg").html("(Vui lòng nhập email giảng viên)");
		isValidInfo = false;
	} else {
		if (!validateEmail($("#txtFacultyEmail").val())){
			$("#emailMsg").html("(Email không hợp lệ)");
			isValidInfo = false;
		}
	}
	
	if ($("#file").val() != ""){
		var path = $("#file").val();
		var fileName = path.substr(path.lastIndexOf('\\') + 1);
		
   		if (fileName.indexOf(".") == -1){
   			$("#fileMsg").html("(Vui lòng chọn file hình ảnh [*.jpg, *.png hoặc *.gif] )");
   			isValidInfo = false;
   		}else{
			var extension = fileName.substr(fileName.lastIndexOf(".") + 1);
			if((extension != "png") && (extension != "PNG")  &&
				(extension != "jpg") && (extension != "JPG") && (extension != "JPEG") && 
				(extension != "gif") && (extension != "GIF")){
				$("#fileMsg").html("(Vui lòng chọn file hình ảnh [*.jpg, *.png hoặc *.gif] )");
				isValidInfo = false;
			}
   		}
	}
	
	if(isValidInfo){
		var mydata = {};
		var path = $("#file").val();
		mydata['image_name'] = path.substr(path.lastIndexOf('\\') + 1);
		
		$.ajax({
	        type: "POST", 
	        url: "/it_center/Faculties/checkImageExist", 
	        data: mydata,
	        success: function(data){  
		        if(data != ""){
			        $("#imageMsg").html("(" + data + ")");
			        return;
		        }else{
	        		return $("#mainform").submit();
		        }
	       	} 
		});
	} 		
}
	
</script>	

<center><H1>Thêm giảng viên</H1></center>
<form id="dummyForm" method="post" action="/it_center/Faculties/index"></form>
<form id="mainform" method="post" enctype="multipart/form-data" action="/it_center/Faculties/faculty_confirm">
<table id="edit_table">
	<tr>
		<td class="edit_column" width="170">Tên giảng viên</td>
		<td style="text-align: left;"><input type="text" name="facultyName" id="txtFacultyName" value="<?php echo $facultyName;?>"/>
		<br/>
		<span id="nameMsg" class="errMsg"></span></td>
	</tr>
	<tr>
		<td class="edit_column">Email</td>
		<td style="text-align: left;"><input type="text" name="facultyEmail" id="txtFacultyEmail" value="<?php echo $facultyEmail;?>"/>
		<br/>
		<span id="emailMsg" class="errMsg"></span></td>
	</tr>
	<tr>
		<td class="edit_column">Điện thoại</td>
		<td style="text-align: left;"><input type="text" name="facultyTel" id="txtFacultyTel" value="<?php echo $facultyTel;?>"/>
		<br/>
		<span id="telMsg" class="errMsg"></span></td>
	</tr>
	<tr>
		<td class="edit_column">Mobile</td>
		<td style="text-align: left;"><input type="text" name="facultyMobile" id="txtFacultyMobile" value="<?php echo $facultyMobile;?>"/>
		<br/>
		<span id="mobileMsg" class="errMsg"></span>	</td>
	</tr>
	<tr>
		<td class="edit_column">Hình ảnh</td>
		<td style="text-align: left;"><input type="file" name="file" id="file">
		<br/>
		<span id="imageMsg" class="errMsg"></span></td>
	</tr>
	<tr>
		<td class="edit_column">Môn học phụ trách</td>
		<td style="text-align: left;">
			<select name="facultySubIds[]" multiple style="width: 100%; height:100px">
			<?php foreach($subjects as $subject):
				echo "<option value='".$subject['Subject']['sub_id']."'>".$subject['Subject']['sub_name']."</option>";
			endforeach;
			?>
			</select>
			<br/>
			<span id="subsMsg" class="errMsg"></span>	
		</td>
	</tr>
	<tr>
		<td class="edit_column" style="vertical-align: top;">Thông tin</td>
		<td style="text-align: left;">
			<textarea name="facultyDesc" id="txtSubDescript" style="width: 99%;" rows="10"><?php echo $facultyDesc;?></textarea>
			<br/>
			<span id="descMsg" class="errMsg"></span>	
		</td>
	</tr>
	<tr>
		<td colspan="2" style="text-align: center;">
			<button type="button" onclick="facultyConfirm()">Xác nhận</button>
			<button type="button" onclick="backToIndex()">Quay Lại</button>
		</td>
	</tr>
</table>
</form>
</section> 
	